<template>
  <div class="catsEdit">
    <h2 class="header">{{id ? '编辑' : '新建'}}管理员</h2>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="用户名">
        <el-input required="required" v-model="model.userName" ></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" required="required" v-model="model.password" ></el-input>
      </el-form-item>
      <el-form-item >
       <el-button type="primary" native-type="submit" style="float: right">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props:{
   id: {
     type: String
   }
  },
  data() {
    return {
      model: {

      },
      parents: [],
      level: [{
        name: '1'
      },
      {
        name: '2'
      },
      {
        name: '3'
      }
      ]
    }
  },
methods: {
  
  async save() {
    let res 
    if (this.id) {
   res = await this.$http.put(`/rest/adminUser/${this.id}`, this.model)
    } else {
    res = await this.$http.post('/rest/adminUser', this.model)

    }
    this.$router.push('/adminUser/list')
    this.$message({
      type: 'success',
      message: '保存成功'
    })
  },
  async fetchParent() {
  const res = await this.$http.get('/genre/level1')
  this.parents = res.data
  },
  async fetchData() {
const res = await this.$http.get(`/rest/adminUser/${this.id}`)
this.model = res.data
  }
},
created() {
  this.fetchParent()
  this.id && this.fetchData()
}
}
</script>

<style>
.header {
  text-align: center;
}
</style>